<form id="purchaseOrder-add-form" onsubmit="purchaseOrder.submitConfirm();return false;">
#include("_form.html")
<div class="row p-3 text-center" style="margin: 0 0 0 0;">
	<div class="col">
		<button type="button" class="btn btn-lg btn-outline-secondary fs-6 btn-wlg" onclick="dashboard.linkTab('/inventory/purchase/order/add')">清空</button>
		&nbsp;&nbsp;&nbsp;
		<button type="button" name="normal_btn" class="btn btn-primary btn-lg fs-6 btn-wlg" onclick="purchaseOrder.normal();">确认保存</button>
		&nbsp;&nbsp;&nbsp;
		<button type="button" name="draft_btn" class="btn btn-outline-primary btn-lg fs-6 btn-wlg" onclick="purchaseOrder.draft();">暂存草稿</button>
	</div>
</div>
</form>

<div id="purchaseOrder-add-confirm" style="display:none;">
		<button type="button" name="normal_btn" class="btn btn-primary btn-lg fs-6" onclick="purchaseOrder.normal();">确认保存</button>
		&nbsp;&nbsp;&nbsp;
		<button type="button" name="draft_btn" class="btn btn-outline-primary btn-lg fs-6" onclick="purchaseOrder.draft();">暂存草稿</button>
		&nbsp;&nbsp;&nbsp;
		<button type="button" class="btn btn-outline-primary btn-lg fs-6" data-bs-dismiss="modal" aria-label="Close">取消</button>
</div>

<script type="application/javascript">
if(typeof(purchaseOrder) == "undefined") {
	purchaseOrder = {};
}

$(function() {
	var formId = "#purchaseOrder-add-form";
	// 初始化bootstrap相关组件
	common.initBootstrap();
	// 绑定tablink
	dashboard.bindTablink();
	
	setTimeout(function() {
		purchaseOrder.handlerList(formId);
		purchaseOrder.supplierList(formId);
		purchaseOrder.bindGoodsSelector(formId);
		purchaseOrder.balanceAccountList(formId);
		
		formInput.listenkeyUp(formId);
		
	}, 500);
	
	#if(purchaseOrder)
		setTimeout(function() {
			$(formId).find("input[name=remark]").first().focus();
		}, 500);
	#end

	purchaseOrder.uploadFileListener(formId)
	
});

purchaseOrder.submitConfirm = function() {
	var formId = "#purchaseOrder-add-form";
	var result = purchaseOrder.formChecking(formId);
	if(!result) {
		return;
	}
	beekit.modalConfirm("确定要保存进货单吗？", $("#purchaseOrder-add-confirm").html());
};

purchaseOrder.normal = function() {
	var formId = "#purchaseOrder-add-form";
	$(formId).find("input[name=order_status]").first().val(#(OrderStatusEnum.normal.value));
	purchaseOrder.create();
};

purchaseOrder.draft = function() {
	var formId = "#purchaseOrder-add-form";
	$(formId).find("input[name=order_status]").first().val(#(OrderStatusEnum.draft.value));
	purchaseOrder.create();
};


purchaseOrder.create = function(msg) {
	var formId = "#purchaseOrder-add-form";
	var result = purchaseOrder.formChecking(formId);
	if(!result) {
		return;
	}
	#if(purchaseOrder && purchaseOrder.id && purchaseOrder.id > 0)
	var url = "/inventory/purchase/order/update";
	#else
	var url = "/inventory/purchase/order/create";
	#end
	
	beekit.ajaxPost(url, $(formId).serialize(), function(ret) {
		common.processRequestResult(ret, function(ret) {
			beekit.modalConfirmClose();
			beekit.msg(ret);
			if (ret.state == "ok") {
				var orderStatusVal = $(formId).find("input[name=order_status]").first().val();
				if(orderStatusVal == #(OrderStatusEnum.draft.value)) {
					beekit.flyer($(formId).find("button[name=draft_btn]").first(), $(formId).find("span[name=draft_count]").first(), "#purchaseOrder-add-form button[name=draft_count_btn]");
				} else {
					beekit.flyer($(formId).find("button[name=normal_btn]").first(), "#inventory_purchase_order");
					#if(orderPrintConfig.purchase_order_flag)
					#if(orderPrintConfig.purchase_order_confirm)
						setTimeout(function() {
							beekit.confirm('是否打印单据？', function() {
								lodopPrinter.purchaseOrderPrint(0, ret.targetId, false);
							});
						}, 1500);
					#else
					lodopPrinter.purchaseOrderPrint(0, ret.targetId, false);
					#end
					#end
				}
				dashboard.linkTab("/inventory/purchase/order/add");
			}	
		});
	});
};

purchaseOrder.draftIndex = function() {
	beekit.offcanvasMiddle("/inventory/purchase/order/draftIndex");
};


</script>
